package org.mspring.mlog.event.handler;

import com.google.common.eventbus.Subscribe;
import org.mspring.mlog.cache.ArticleCacheService;
import org.mspring.mlog.event.BaseEventHandler;
import org.mspring.mlog.event.event.ArticleCreateEvent;
import org.mspring.mlog.event.event.ArticleDeleteEvent;
import org.mspring.mlog.event.event.ArticleUpdateEvent;
import org.mspring.mlog.search.ArticleSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Gao Youbo
 * @since 2015-11-07 14:45
 */
@Component
public class ArticleEventHandler implements BaseEventHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ArticleEventHandler.class);


    private ArticleCacheService articleCacheService;
    private ArticleSearchService articleSearchService;

    @Subscribe
    public void create(ArticleCreateEvent event) {
        LOG.info("article create event.");

        // 构建文章索引
        articleSearchService.addIndex(event.getArticleId());
    }

    @Subscribe
    public void update(ArticleUpdateEvent event) {
        LOG.info("article update event.");

        // 构建文章索引
        articleSearchService.addIndex(event.getArticleId());
    }

    @Subscribe
    public void delete(ArticleDeleteEvent event) {
        LOG.info("article delete event.");

        // 构建文章索引
        articleSearchService.addIndex(event.getArticleId());
    }

    @Autowired
    public void setArticleCacheService(ArticleCacheService articleCacheService) {
        this.articleCacheService = articleCacheService;
    }

    @Autowired
    public void setArticleSearchService(ArticleSearchService articleSearchService) {
        this.articleSearchService = articleSearchService;
    }
}
